package be.jabapage.util;

import javax.jdo.PersistenceManagerFactory;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import be.jabapage.snooker.service.administration.api.IClubService;
import be.jabapage.snooker.service.administration.api.IFormatParser;
import be.jabapage.snooker.service.administration.api.IPlayerService;
import be.jabapage.snooker.service.administration.api.IRegionService;
import be.jabapage.snooker.service.administration.api.ITeamService;
import be.jabapage.snooker.service.administration.impl.AppEngineClubService;
import be.jabapage.snooker.service.administration.impl.AppEnginePlayerService;
import be.jabapage.snooker.service.administration.impl.AppEngineRegionService;
import be.jabapage.snooker.service.administration.impl.AppEngineTeamService;
import be.jabapage.snooker.service.administration.impl.FormatParser;
import be.jabapage.snooker.service.importation.api.IClubImportService;
import be.jabapage.snooker.service.importation.api.ICompetitionImportService;
import be.jabapage.snooker.service.importation.api.IExcelClubParser;
import be.jabapage.snooker.service.importation.api.IExcelImportService;
import be.jabapage.snooker.service.importation.api.IPlayerImportService;
import be.jabapage.snooker.service.importation.api.IRegionImportService;
import be.jabapage.snooker.service.importation.api.ITeamImportService;
import be.jabapage.snooker.service.importation.impl.ClubImportService;
import be.jabapage.snooker.service.importation.impl.ExcelClubParser;
import be.jabapage.snooker.service.importation.impl.ExcelImportService;
import be.jabapage.snooker.service.importation.impl.PlayerImportService;
import be.jabapage.snooker.service.importation.impl.RegionImportService;
import be.jabapage.snooker.service.importation.impl.TeamImportService;
import be.jabapage.snooker.service.overwriter.api.IClubOverwriter;
import be.jabapage.snooker.service.overwriter.api.IPlayerOverwriter;
import be.jabapage.snooker.service.overwriter.api.IRegionOverwriter;
import be.jabapage.snooker.service.overwriter.api.ITeamOverwriter;
import be.jabapage.snooker.service.overwriter.impl.SimpleClubOverwriter;
import be.jabapage.snooker.service.overwriter.impl.SimplePlayerOverwriter;
import be.jabapage.snooker.service.overwriter.impl.SimpleRegionOverwriter;
import be.jabapage.snooker.service.overwriter.impl.SimpleTeamOverwriter;

@Configuration
public class ServiceWiring {

	@Bean public PersistenceManagerFactory persistenceManagerFactory() {
		return new DummyPersistenceManagerFactory();
	}

	/*
	 * CRUD service
	 */
	@Bean public IRegionService regionService() {
		return new AppEngineRegionService(persistenceManagerFactory());
	}

	@Bean public IClubService clubService() {
		return new AppEngineClubService(persistenceManagerFactory());
	}

	@Bean public ITeamService teamService() {
		return new AppEngineTeamService(persistenceManagerFactory());
	}

	@Bean public IPlayerService playerService() {
		return new AppEnginePlayerService(persistenceManagerFactory());
	}


	/*
	 * Parsers
	 */
	@Bean public IExcelClubParser clubParser() {
		return new ExcelClubParser(formatParser());
	}

	@Bean public IFormatParser formatParser() {
		return new FormatParser("[0-9][0-9][0-9][0-9]/[0-9][0-9][0-9][0-9]", "99[0-9][0-9]", "", "");
	}

	/*
	 * import services
	 */

	@Bean public IExcelImportService importExcelService() {
		return new ExcelImportService(clubParser());
	}

	@Bean public IClubImportService clubImportService() {
		return new ClubImportService(clubService(), simpleClubOverwriter(), teamImportService(), playerImportService());
	}

	@Bean public IPlayerImportService playerImportService() {
		return new PlayerImportService(playerService(), simplePlayerOverwriter());
	}

	@Bean public IRegionImportService regionImportService() {
		return new RegionImportService(regionService(), clubImportService(), simpleRegionOverwriter(), competitionImportService());
	}

	@Bean public ITeamImportService teamImportService() {
		return new TeamImportService(teamService(), playerImportService(), simpleTeamOverwriter());
	}
	
	@Bean public ICompetitionImportService competitionImportService() {
		return null;
	}

	/*
	 * Simple Overwriters
	 */
	@Bean public IRegionOverwriter simpleRegionOverwriter() {
		return new SimpleRegionOverwriter();
	}

	@Bean public IClubOverwriter simpleClubOverwriter() {
		return new SimpleClubOverwriter();
	}

	@Bean public IPlayerOverwriter simplePlayerOverwriter() {
		return new SimplePlayerOverwriter();
	}

	@Bean public ITeamOverwriter simpleTeamOverwriter() {
		return new SimpleTeamOverwriter();
	}
}

